﻿using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using SupportFragmentActivity = Android.Support.V4.App.FragmentActivity;
using SupportFragmentManager = Android.Support.V4.App.FragmentManager;
using SupportFragmentTransaction = Android.Support.V4.App.FragmentTransaction;
using jtgApp.fragmentActivte;

namespace jtgApp
{
    [Activity(Label = "jtgApp", Icon = "@drawable/icon", MainLauncher = true)]
    public class MainActivity : SupportFragmentActivity, View.IOnClickListener
    {
        //UI Object
        private TextView txt_channel;
        private TextView txt_message;
        private TextView txt_better;
        private TextView txt_setting;
        private FrameLayout ly_content;

        //Fragment Object
        private myFragment fg3, fg4;
        private indexFragment fg1;
        private hotFragment fg2;
        SharedConfig sc = new SharedConfig();
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            this.SetTheme(Android.Resource.Style.ThemeNoTitleBarFullScreen);
            SetContentView(Resource.Layout.activityMain);            
            bindViews();
            txt_channel.PerformClick(); 
        }
       
        //UI组件初始化与事件绑定
        private void bindViews()
        {
            txt_channel = (TextView)FindViewById(Resource.Id.txt_channel);
            txt_message = (TextView)FindViewById(Resource.Id.txt_message);
            txt_better = (TextView)FindViewById(Resource.Id.txt_better);
            txt_setting = (TextView)FindViewById(Resource.Id.txt_setting);
            ly_content = (FrameLayout)FindViewById(Resource.Id.ly_content);

            txt_channel.SetOnClickListener(this);
            txt_message.SetOnClickListener(this);
            txt_better.SetOnClickListener(this);
            txt_setting.SetOnClickListener(this);
            var menuButtonLeft = FindViewById(Resource.Id.MenuButtonLeft);
            menuButtonLeft.Click += MenuButtonLeft_Click;
        }
        bool leftMenuLR = true;
        private void MenuButtonLeft_Click(object sender, EventArgs e)
        {       
            if(leftMenuLR)
            {
                FindViewById(Resource.Id.MenuLeft).TranslationX = 0;
                FindViewById(Resource.Id.MainContent).SetX(FindViewById(Resource.Id.MenuLeft).Width);
                leftMenuLR = false;
            }    
                              
            else
            {
                FindViewById(Resource.Id.MenuLeft).TranslationX = -FindViewById(Resource.Id.MenuLeft).Width;
                FindViewById(Resource.Id.MainContent).SetX(0);
                leftMenuLR = true;
            }
        }

        //重置所有文本的选中状态
        private void setSelected()
        {
            txt_channel.Selected = false;
            txt_message.Selected = false;
            txt_better.Selected = false;
            txt_setting.Selected = false;
        }

        //隐藏所有Fragment
        private void hideAllFragment(SupportFragmentTransaction fragmentTransaction)
        {
            if (fg1 != null) fragmentTransaction.Hide(fg1);
            if (fg2 != null) fragmentTransaction.Hide(fg2);
            if (fg3 != null) fragmentTransaction.Hide(fg3);
            if (fg4 != null) fragmentTransaction.Hide(fg4);
        }
        public void OnClick(View v)
        {
            var fManager = SupportFragmentManager;
            var fTransaction = fManager.BeginTransaction();
            hideAllFragment(fTransaction);
            switch (v.Id)
            {
                case Resource.Id.txt_channel:
                    setSelected();
                    txt_channel.Selected = true;
                    if (fg1 == null)
                    {
                        fg1 = new indexFragment();
                        fTransaction.Add(Resource.Id.ly_content, fg1);
                    }
                    else
                    {
                        fTransaction.Show(fg1);
                    }
                    break;
                case Resource.Id.txt_message:
                    setSelected();
                    txt_message.Selected = true;
                    if (fg2 == null)
                    {
                        fg2 = new hotFragment("第二个Fragment");
                        fTransaction.Add(Resource.Id.ly_content, fg2);
                    }
                    else
                    {
                        fTransaction.Show(fg2);
                    }
                    break;
                case Resource.Id.txt_better:
                    setSelected();
                    txt_better.Selected = true;
                    if (fg3 == null)
                    {
                        fg3 = new myFragment("第三个Fragment");
                        fTransaction.Add(Resource.Id.ly_content, fg3);
                    }
                    else
                    {
                        fTransaction.Show(fg3);
                    }
                    break;
                case Resource.Id.txt_setting:
                    setSelected();
                    txt_setting.Selected = true;
                    if (fg4 == null)
                    {
                        fg4 = new myFragment("第四个Fragment");
                        fTransaction.Add(Resource.Id.ly_content, fg4);
                    }
                    else
                    {
                        fTransaction.Show(fg4);
                    }
                    break;
            }
            fTransaction.Commit();
        }

    }
}
